package cn.javass.dp.memento.example3;

import java.io.Serializable;

/**
 * 模拟运行流程A，示意，代指某个具体流程
 * 文件存储需要序列化
 */
public class FlowAMock implements Serializable {
    /**
     * 流程名词，不需要外部存储的状态数据
     */
    private String flowName;

    private int tmpResult;

    private String tmpState;

    /**
     * 构造方法，传入流程名词
     * @param flowName
     */
    public FlowAMock(String flowName) {
        this.flowName = flowName;
    }

    public void runPhaseOne() {
        //在这个阶段，可能产生了中间结果，示意一下
        tmpResult = 3;
        tmpState = "PhaseOne";
    }

    public void schema1() {
        this.tmpState += ",Schema1";
        System.out.println(this.tmpState+"now run" + tmpResult);
        this.tmpResult += 11;
    }

    public void schema2() {
        this.tmpState += ",Schema2";
        System.out.println(this.tmpState+"now run" + tmpResult);
        this.tmpResult += 22;
    }

    /**
     * 创建保存 原发器对象的状态的备忘录对象
     * @return
     */
    public FlowAMockMemento createMemento() {
        return new MementoImpl(this.tmpResult, this.tmpState);
    }

    /**
     * 重新设置原发器对象的状态，让其回到备忘录对象记录的状态
     * @param memento
     */
    public void setMemento(FlowAMockMemento memento) {
        MementoImpl mementoImpl = (MementoImpl) memento;
        this.tmpResult = mementoImpl.getTmpResult();
        this.tmpState = mementoImpl.getTmpState();
    }


    private class MementoImpl implements FlowAMockMemento {
        private int tmpResult;
        private String tmpState;

        public MementoImpl(int tmpResult, String tmpState) {
            this.tmpResult = tmpResult;
            this.tmpState = tmpState;
        }

        public int getTmpResult() {
            return tmpResult;
        }

        public String getTmpState() {
            return tmpState;
        }
    }
}
